<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    unit: {
      type: String,
      default: ""
    },
    chartData: {
      type: Array,
      required: true
    },
    chartTime: {
      type: Array,
      required: true
    },
    min: {
      type: Number,
      required: true
    },
    max: {
      type: Number,
      required: true
    },
    interval: {
      type: Number,
      required: true
    },
    markLineValue1: {
      type: Number,
      required: false
    },
    markLineValue2: {
      type: Number,
      required: false
    },
    markLineValue3: {
      type: Number,
      required: false
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.unit,
          this.chartData,
          this.chartTime,
          this.min,
          this.max,
          this.interval,
          this.markLineValue1,
          this.markLineValue2,
          this.markLineValue3
        );
      }
    },
    chartTime: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.unit,
          this.chartData,
          this.chartTime,
          this.min,
          this.max,
          this.interval,
          this.markLineValue1,
          this.markLineValue2,
          this.markLineValue3
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
        this.headTitle,
        this.unit,
        this.chartData,
        this.chartTime,
        this.min,
        this.max,
        this.interval,
        this.markLineValue1,
        this.markLineValue2,
        this.markLineValue3
      );
    },
    setOptions(title, unit, chartData, chartTime, min, max, interval, markLineValue1, markLineValue2, markLineValue3) {
      this.chart.setOption({
        title: {
          text: title,
          left: "center"
        },
        grid: {
          left: 20,
          right: 35,
          bottom: 10,
          top: 35,
          containLabel: true
        },
        xAxis: {
          data: chartTime.map(function (str) {
            return str.replace(' ', '\n');
          }),
          boundaryGap: false, //falseX轴两边对齐
          //坐标轴刻度相关设置
          axisTick: {
            //是否显示坐标轴刻度
            show: false
          },
        },
        yAxis: {
          type: "value",
          name: "单位：" + unit,
          nameLocation: "end",
          min: function (value) {//取最小值向下取整为最小刻度
            return Math.floor(value.min)
          },
          max: function (value) {//取最大值向上取整为最大刻度
            return Math.ceil(value.max)
          },
          boundaryGap: false,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true
          },
          axisLabel: {
            formatter: '{value} '
          }
          /*axisLabel: {
            formatter: function (value) {
              return parseInt(value)
            }
          }*/
        },
        //提示框组件
        tooltip: {
          //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
          trigger: "axis",
          //坐标轴指示器配置项
          axisPointer: {
            //十字准星指示器
            type: "line",//shadow阴影效果
            axis: 'x',
            //坐标轴指示器的文本标签
            label: {
              //文本标签中数值的小数点精度
              precision: 0
            }
          },
          formatter: '{b0}<br />' + title + '：{c0}' + unit,
          padding: [5, 10]
        },
        series: [
          {
            //name: title,
            smooth: true,     // 这个可以显示为平滑过渡的线条
            symbol: "circle", // 这个把节点的空心圆换成实心圆
            symbolSize: 1,    // 节点大小，数字随意，跟下面的线宽一样或少 1 像素
            type: "line",
            //折线拐点标志的样式
            itemStyle: {
              color: "#1890FF"
            },
            //线条样式
            lineStyle: {
              type: "dotted"
            },
            data: chartData,
            markLine: {
              symbol: "none",//是否显示首尾箭头'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'
              data: [
                {
                  yAxis: markLineValue3,
                  lineStyle: {
                    type: "dashed",
                    color: "#ff512d",
                    width: 2,
                  }
                }, {
                  yAxis: markLineValue2,
                  lineStyle: {
                    type: "dashed",
                    color: "#0fc821",
                    width: 2,
                  }
                },
                {
                  yAxis: markLineValue1,
                  lineStyle: {
                    type: "dashed",
                    color: "#7aabd6",
                    width: 2,
                  }
                }
              ]
            },
            animationDuration: 2800,
            animationEasing: "cubicInOut"
          }
        ]
      });
    }
  }
};
</script>
